import requests
from loguru import logger
from config import bookcps_token


def get_details_api(action_name, api_base_url, item_id):
    res_desc = requests.get(api_base_url, params={"data_id": item_id})
    logger.debug(res_desc.url)
    if action_name == "article":
        data_info_desc = res_desc.json()["info"]
    else:
        if not res_desc.json()["list"]:
            return []
        else:
            data_info_desc = res_desc.json()["list"][0]

    return {
        "detail_id": data_info_desc["id"],
        "info_title": data_info_desc["title"],
        "cover": data_info_desc["cover"],
        "category_id": data_info_desc["category_id"],
        "name": data_info_desc.get("name"),
    }


def cps_books_api(action_name, api_base_url, item_id):
    headers = {"token": bookcps_token}
    res = requests.get(api_base_url, headers=headers, params={"id": item_id})
    res_data = res.json()
    if not res_data["data"]:
        return {}
    else:
        return {
            "detail_id": res_data["data"]["id"],
            "info_title": res_data["data"]["title"],
            "category_id": res_data["data"]["new_cate_id"],
            "cover": res_data["data"]["image_url"],
            "name": res_data["data"].get("cate_name"),
        }


def search_api(create_time, user_id, api_base_url, item_id):
    item_dict = {
        1: "首页搜索",
        2: "变现搜索",
        3: "素材中心搜索",
        4: "课程搜索",
        5: "合同搜索",
        6: "看大盘搜索",
    }
    search_type = item_dict[item_id]
    # https://scapi.tayunapi.com/funcLog/searchList?page=1&rows=50&user_id=2210911&create_time=1716826405
    res = requests.get(
        api_base_url,
        params={
            "user_id": user_id,
            "create_time": create_time,
            "rows": 10,
        },
    )
    logger.debug(res.url)
    if res.json()["list"]:
        data = res.json()["list"][0]
        data["search_type"] = search_type
        data["action"] = "search"
        return data
    else:
        return []
    # 根据需求实现
